/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
 */

package com.jinhe.tss.component.recycle.maintain;

import java.util.List;

import com.jinhe.tss.component.recycle.entity.Recycle;
import com.jinhe.tss.core.persistence.BaseDao;

/**
 * <p> RecycleDao.java </p>
 * <p>
 * Recycle的Dao层，负责处理Recycle相关的数据库操作
 * </p>
 */
public class RecycleDaoImpl extends BaseDao<Recycle> implements RecycleDao {

    public RecycleDaoImpl() {
        super(Recycle.class);
    }

    public void recoverByRecycleId(String tableName, Long recycleId) {
        executeHQL("update " + tableName + " t set t.recycleId=null, t.deleted=0 where t.recycleId= ? ", recycleId);
    }

    public void recoverByEntityId(String tableName, Long nodeId) {
        executeHQL("update " + tableName + " t set t.recycleId='',t.deleted=? where t.id=?", Recycle.FALSE, nodeId);
    }

    public void recoverParent(String tableName, Long parentId) {
        executeHQL("update " + tableName + " t set t.deleted=? where t.id=?", Recycle.FALSE, parentId);
    }

    public List<?> getParentNodes(String tableName, Long id) {
        String hql = "select m.id, m.deleted from " + tableName + " m, " + tableName + " m1 " +
                "where m1.decode like m.decode||'%' and m1.id = ?";
        return getEntities(hql, id);
    }

    public List<?> getNodesByRecycleId(String tableName, Long recycleId) {
        String hql = "select t.id from " + tableName + " t where t.recycleId = ?";
        return getEntities(hql, recycleId);
    }

    public List<?> getRecycleList(Integer typeId) {
        return getEntities("from Recycle r where r.typeId=? order by r.deleteDate", typeId);
    }
}
